package com.liser.southernstar.utils;

import java.util.Random;

public class Password {
    /**
     * 生成随机ASCII可显示字符。可用于随机密码
     * <p>create/modify by 潇湘夜雨  2019年10月1日</p>
     * @return
     */
    public static String randomChar(int len){
        Random random = new Random((long)(Long.MAX_VALUE * Math.random()));
        char[] buffer = new char[len];
        for (int i = 0; i < len; i++){
            buffer[i] = (char)(random.nextFloat() * (127-32) + 32); // ASCII可显示区间为[32, 126]
        }
        return new String(buffer);
    }

    /**
     * 生成随机byte数组。可用于对称加解密的强力密匙
     * @return
     */
    public static byte[] randomBytes(int len){
        Random random = new Random((long)(Long.MAX_VALUE * Math.random()));
        byte[] bytes = new byte[len];
        for (int i = 0, max = Byte.MAX_VALUE + 1; i < len; i++){
            bytes[i] = (byte)(random.nextFloat() * max);
        }
        return bytes;
    }
}
